package com.financing.wap.web.controller.api;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.financing.wap.service.*;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.map.Module.SetupContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.common.ResultMap.STATUS;
import com.financing.wap.common.SessionUtil;
import com.financing.wap.dto.FpFundInfoDTO;
import com.financing.wap.dto.FpShopperProductDTO;
import com.financing.wap.dto.ProductBasicInfoDTO;
import com.financing.wap.dto.ShopperUserInfoDTO;
import com.financing.wap.dto.UserDTO;
import com.financing.wap.util.DateUtil;
import com.financing.wap.util.StringUtil;
import com.financing.wap.vo.ShopperProductVO;
import com.financing.wap.web.controller.BaseController;

@Controller("shopperProductController")
@RequestMapping("/api/shopperProduct")
public class ShopperProductController extends BaseController{
	
	@Autowired
	IFpShopperProductService fpShopperProductService;
	@Autowired
	private IProductService productService;
	@Autowired
	private IFpFundInfoService fpFundInfoService;
	
	@Autowired
	private IUserService userService;
	
	@Autowired
	private IShopperUserInfoService shopperUserInfoService;

	@Autowired
	private IFpShopUserRelProductService fpShopUserRelProductService;
	
	/**
	 * 
	
	* @Title: addShopProductShare
	
	* @Description: 分享用户店铺添加产品
	
	* @param @param request
	* @param @return    设定文件
	
	* @return ResultMap    返回类型
	
	* @throws
	 */
	@RequestMapping(value="/addShopProductShare")
	@ResponseBody
	public ResultMap addShopProductShare(HttpServletRequest request){
		ResultMap result = new ResultMap();
		
		Boolean login = SessionUtil.getSession().getLogin();
		
		if(login){
			addShopProduct(request);
		}
		result.setStatus(STATUS.SUCCESS);
		result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
		return result;
	}
	/**
	 * 
	
	* @Title: addShopProduct
	
	* @Description: 用户店铺添加产品
	
	* @param @param request
	* @param @return    设定文件
	
	* @return ResultMap    返回类型
	
	* @throws
	 */
	@RequestMapping(value="/addShopProduct")
	@ResponseBody
	public ResultMap addShopProduct(HttpServletRequest request){
		ResultMap result = new ResultMap();

		try {
			String userId = SessionUtil.getSession().getUser().getId();
			String productId=request.getParameter("productId");
			String productType=request.getParameter("productType");
			if(StringUtil.isNullOrEmptyNStr(productId,productType)){
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				return result;
			}
			FpShopperProductDTO sp=new FpShopperProductDTO();
			sp.setProductId(productId);
			sp.setUserId(userId);
			sp.setProductType(productType);
			sp.setDeleteTag("1");
			FpShopperProductDTO old = fpShopperProductService.queryListGetFirst(sp);
			if(old.getId()!=null){
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultMap.STATUS_CODE_REPEAT_ADD);
				return result;
			}
			sp.setFlagTuijian("2");//默认为不推荐
			sp.setProductState(1);//默认为上架状态
			fpShopperProductService.insert(sp);
			result.setStatus(STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("addShopProduct error",e);
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	/**
	 * 
	
	* @Title: shopProductTuijian
	
	* @Description: 用户店铺产品推荐修改
	
	* @param @param request
	* @param @return    设定文件
	
	* @return ResultMap    返回类型
	
	* @throws
	 */
	@RequestMapping(value="/shopProductTuijian")
	@ResponseBody
	public ResultMap shopProductTuijian(HttpServletRequest request,FpShopperProductDTO dto){
		ResultMap result = new ResultMap();

		try {
			String userId = SessionUtil.getSession().getUser().getId();
			String productId=dto.getProductId();
			String productType=dto.getProductType();
			String flagTuijian=dto.getFlagTuijian();
			String tuijianReson=dto.getTuijianReson();
			if(StringUtil.isNullOrEmptyNStr(productId,productType)){
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				return result;
			}
			if(StringUtil.isNullOrEmptyNStr(flagTuijian)){//默认取消推荐
				flagTuijian="2";
			}
			FpShopperProductDTO sp=new FpShopperProductDTO();
			sp.setProductId(productId);
			sp.setUserId(userId);
			sp.setProductType(productType);
			sp.setDeleteTag("1");
			
			FpShopperProductDTO old = fpShopperProductService.queryListGetFirst(sp);
			if(old.getId()!=null){
				old.setFlagTuijian(flagTuijian);
				if("1".equals(flagTuijian)){
					old.setTuijianReson(tuijianReson);
				}else{//取消推荐  推荐理由置空
					old.setTuijianReson("");
				}
				//产品标签
				old.setProductTag(dto.getProductTag());
				//输入100以内数字，数字越大，优先级越高;店铺首页显示优先级最高的5款产品
				old.setProductPriority(dto.getProductPriority());
				fpShopperProductService.updateForce(old);
			}
			
			result.setStatus(STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
		} catch (Exception e) {
			log.error("shopProductTuijian error",e);
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	/**
	 * 
	
	* @Title: shopProductDetail
	
	* @Description: 用户店铺产品查询推荐信息
	
	* @param @param request
	* @param @return    设定文件
	
	* @return ResultMap    返回类型
	
	* @throws
	 */
	@RequestMapping(value="/shopProductDetail")
	@ResponseBody
	public ResultMap shopProductDetail(HttpServletRequest request,FpShopperProductDTO dto){
		ResultMap result = new ResultMap();

		try {
			String userId = SessionUtil.getSession().getUser().getId();
			String productId=dto.getProductId();
			String productType=dto.getProductType();
			if(StringUtil.isNullOrEmptyNStr(productId,productType)){
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				return result;
			}
			FpShopperProductDTO sp=new FpShopperProductDTO();
			sp.setProductId(productId);
			sp.setUserId(userId);
			sp.setProductType(productType);
			sp.setDeleteTag("1");
			FpShopperProductDTO old = fpShopperProductService.queryListGetFirst(sp);
			//查询产品名字
			if("01".equals(productType)){
				//基金
				FpFundInfoDTO fpFundInfo = fpFundInfoService.getFpFundInfo(Integer.parseInt(productId));
				old.setpName(fpFundInfo.getJjmc());
			}else{
				//非基金
				ProductBasicInfoDTO queryBasicInfo = productService.queryBasicInfo(productId);
				old.setpName(queryBasicInfo.getName());
			}
			result.setAttribute("className", ShopperProductController.class.getName());
			result.setAttribute("methodName", "shopperproduct");
			result.setAttribute("dto", old);
			result.setStatus(STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
		} catch (Exception e) {
			log.error("shopProductDetail error",e);
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	/** 放射 wap　过滤数据
	 * @param json
	 * @return
	 */
	public String wapshopperproduct(String jsons){
		try {
			JSONObject json = JSON.parseObject(jsons);
			
			//获取  类名 方法名  进行放射
			JSONObject jo=json.getJSONObject("attributes");
			JSONObject dto = jo.getJSONObject("dto");
			//进行 多余字段过滤的数据
			dto.remove("createDate");
			dto.remove("createBy");
			dto.remove("updateDate");
			dto.remove("updateBy");
			dto.remove("deleteTag");
			return json.toString();
		} catch (Exception e) {
			log.error("error  wapshopperproduct:",e);
			return jsons;
		}
	}

	/**
	 * 
	
	* @Title: shopProductTop
	
	* @Description: 用户店铺产品置顶
	
	* @param @param request
	* @param @return    设定文件
	
	* @return ResultMap    返回类型
	
	* @throws
	 */
	@RequestMapping(value="/shopProductTop")
	@ResponseBody
	public ResultMap shopProductTop(HttpServletRequest request){
		ResultMap result = new ResultMap();

		try {
			String userId = SessionUtil.getSession().getUser().getId();
			String productId=request.getParameter("productId");
			String productType=request.getParameter("productType");
			if(StringUtil.isNullOrEmptyNStr(productId,productType)){
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				return result;
			}
			FpShopperProductDTO sp=new FpShopperProductDTO();
			sp.setProductId(productId);
			sp.setUserId(userId);
			sp.setProductType(productType);
			sp.setDeleteTag("1");
			
			FpShopperProductDTO old = fpShopperProductService.queryListGetFirst(sp);
			if(old.getId()!=null){
				old.setTopDate(DateUtil.getDate("yyyyMMddHHmmss"));
				fpShopperProductService.update(old);
			}
			
			result.setStatus(STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
		} catch (Exception e) {
			log.error("shopProductTuijian error",e);
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	/***
	 * 
	
	* @Title: delShopProduct
	
	* @Description: 用户店铺删除产品
	
	* @param @param request
	* @param @return    设定文件
	
	* @return ResultMap    返回类型
	
	* @throws
	 */
	@RequestMapping(value="/delShopProduct")
	@ResponseBody
	public ResultMap delShopProduct(HttpServletRequest request){
		ResultMap result = new ResultMap();

		try {
			String userId = SessionUtil.getSession().getUser().getId();
			String productId=request.getParameter("productId");
			String productType=request.getParameter("productType");
			if(StringUtils.isBlank(productId)){
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				return result;
			}
			FpShopperProductDTO sp=new FpShopperProductDTO();
			sp.setProductId(productId);
			sp.setUserId(userId);
			sp.setProductType(productType);
			sp.setDeleteTag("2");
			fpShopperProductService.update(sp);
			result.setStatus(STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
		} catch (Exception e) {
			log.error("delShopProduct error",e);
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		return result;
	}
	
	/***
	 * 
	
	* @Title: shopProductList
	
	* @Description: 用户店铺列表产品
	
	* @param @param request
	* @param @return    设定文件
	
	* @return ResultMap    返回类型
	
	* @throws
	 */
	@RequestMapping(value="/shopProductList")
	@ResponseBody
	public ResultMap shopProductList(HttpServletRequest request,Pager pager){
		ResultMap result = new ResultMap();
		try {
			String userId = null;
			
			String loginMobile=request.getParameter("mobile");
			
			if(StringUtils.isBlank(loginMobile)){
				//查询当前用户的活动
				UserDTO u=SessionUtil.getSession().getUser();
				if(null == u){
					result.setStatusCode(ResultMap.LOGIN_NOLOGIN_ERROR);
					result.setStatus(ResultMap.STATUS.FAILE);
					return result;
				}
				//查询当前用户的说说
				userId=u.getId();
			}else{
				UserDTO dto=new UserDTO();
				dto.setLoginName(loginMobile);
				UserDTO u = userService.queryUserLoginName(dto);
				userId = u.getId();
			}
			
			FpShopperProductDTO shop=new FpShopperProductDTO();
			String proState = request.getParameter("productState");
			if(StringUtils.isBlank(proState)){
				shop.setProductState(1);  //默认为上架的
			}else{
				shop.setProductState(Integer.valueOf(proState));
			}
			
			
			int pageNo=pager.getPageNo();
			shop.setPageNo(pageNo);
			int pageSize=pager.getPageSize();
			shop.setPageSize(pageSize);
			shop.setUserId(userId);
			int count = fpShopperProductService.queryShopperProductCount(shop);
			pager.setTotalCount(count);
			if(count>0){
				List<ShopperProductVO> queryShopperProduct = fpShopperProductService.queryShopperProduct(shop,null);
				result.setAttribute("list", queryShopperProduct);
				ShopperUserInfoDTO shopperUserInfo=new ShopperUserInfoDTO();
				//用户id
				shopperUserInfo.setUserId(userId);
				ShopperUserInfoDTO dto = shopperUserInfoService.queryListGetFirst(shopperUserInfo);
				result.setAttribute("shopperUserInfo",dto);
			}else{
				result.setAttribute("list", Collections.EMPTY_LIST);
			}
			result.setAttribute("pager", pager);
			result.setStatus(STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("shopProductList error",e);
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		return result;
	}
	/***
	 *

	 * @Title: shopProductList

	 * @Description: 用户店铺列表产品

	 * @param @param request
	 * @param @return    设定文件

	 * @return ResultMap    返回类型

	 * @throws
	 */
	@RequestMapping(value="/shopProductSelfDetail")
	@ResponseBody
	public ResultMap shopProductSelfDetail(HttpServletRequest request,Pager pager,Integer pId){
		ResultMap result = new ResultMap();
		if(pId == null){
			result.setStatus(STATUS.FAILE);
			result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
			return result;
		}


		return result;
	}
	
	/***
	 * 
	
	* @Title: shopProductList
	
	* @Description: 上下架产品
	
	* @param @param request
	* @param @return    设定文件
	
	* @return ResultMap    返回类型
	
	* @throws
	 */
	@RequestMapping(value="/upOrDownProduct")
	@ResponseBody
	public ResultMap upOrDownProduct(HttpServletRequest request){
		ResultMap result = new ResultMap();
		try {
			String userId = null;
			
			String loginMobile=request.getParameter("mobile");
			String productState = request.getParameter("status");
			String proid = request.getParameter("proid");
			String protype = request.getParameter("protype");
			
			if(StringUtils.isBlank(loginMobile)){
				//查询当前用户的活动
				UserDTO u=SessionUtil.getSession().getUser();
				if(null == u){
					result.setStatusCode(ResultMap.LOGIN_NOLOGIN_ERROR);
					result.setStatus(ResultMap.STATUS.FAILE);
					return result;
				}
				//查询当前用户的说说
				userId=u.getId();
			}else{
				UserDTO dto=new UserDTO();
				dto.setLoginName(loginMobile);
				UserDTO u = userService.queryUserLoginName(dto);
				userId = u.getId();
			}
			
			FpShopperProductDTO shop=new FpShopperProductDTO();
			shop.setProductId(proid);
			shop.setProductType(protype);
			shop.setUserId(userId);
			shop.setProductState(Integer.parseInt(productState));
			fpShopperProductService.update(shop);
			
			result.setStatus(STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("shopProductList error",e);
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		return result;
	}
}
